package com.popcorn.member.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.popcorn.member.model.MemberVo;

public class MemberDAO {

	Connection conn = null;
	PreparedStatement pstmt = null;
	DataSource ds = null;
	ResultSet rs = null;
	
	public MemberDAO(){
	try{
		Context init = new InitialContext();
		ds = (DataSource)init.lookup("java:comp/env/jdbc/Mysql");
		         
	}catch(Exception e){
		e.printStackTrace();
		}	  
	}
	
	public boolean id_check(String id) {
		String sql = "";
		try{
			try {
				conn = ds.getConnection();				
			} catch (Exception e) {
				System.out.println("connect Exception : "+e.toString());
			}
			sql = "select * from member where ID='"+id+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			System.out.println(sql);
			if(rs.next()) {
				System.out.println("중복됨");
				return false;
			}
			
			return true;
			
		}catch(Exception ex){
			System.out.println("?�록 ?�패");
			ex.printStackTrace();
		}finally{
			if(rs!=null) try{rs.close();}catch(SQLException ex){}
			if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
			if(conn!=null) try{conn.close();}catch(SQLException ex){}
		}
		return false;
	}

	public boolean nickname_check(String nickname) {
		String sql = "";
		try{
			try {
				conn = ds.getConnection();				
			} catch (Exception e) {
				System.out.println("connect Exception : "+e.toString());
			}
			sql = "select * from member where NICKNAME='"+nickname+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			System.out.println(sql);
			if(rs.next()) {
				System.out.println("중복됨");
				return false;
			}
			
			return true;
			
		}catch(Exception ex){
			System.out.println("?�록 ?�패");
			ex.printStackTrace();
		}finally{
			if(rs!=null) try{rs.close();}catch(SQLException ex){}
			if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
			if(conn!=null) try{conn.close();}catch(SQLException ex){}
		}
		return false;
	}

	public boolean insertVo(MemberVo vo) {
		int result = 0;
		int num =0;
		String sql="";			
		
		try{
			try {
				conn = ds.getConnection();				
			} catch (Exception e) {
				System.out.println("connect Exception : "+e.toString());
			}
			pstmt=conn.prepareStatement("select max(MEMBER_SEQ) from member");
			rs = pstmt.executeQuery();
			
			if(rs.next())
				num =rs.getInt(1)+1;
			else
				num=1;
			// 날짜 시간 겹치지 않게 등록 + 방 크기
			sql="insert into member values(?,?,?,?,MD5(?),?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, num);
			pstmt.setString(2, vo.getId());
			pstmt.setString(3, vo.getName());
			pstmt.setString(4, vo.getNickname()); 
			pstmt.setString(5, vo.getPwd());
			pstmt.setString(6, vo.getHp());
			
			System.out.println("====================InsertDB====================");
			System.out.println("1 : "+num);
			System.out.println("2 : "+vo.getId());
			System.out.println("3 : "+vo.getName());
			System.out.println("4 : "+vo.getNickname());
			System.out.println("5 : "+vo.getPwd());
			System.out.println("6 : "+vo.getHp());
			result=pstmt.executeUpdate();
			
			if(result==0)return false;
			
			return true;
			
		}catch(Exception ex){
			System.out.println("등록 실패");
			ex.printStackTrace();
		}finally{
			if(rs!=null) try{rs.close();}catch(SQLException ex){}
			if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
			if(conn!=null) try{conn.close();}catch(SQLException ex){}
		}
		return false;
	}

	public MemberVo getUserItem(String user) {
		String sql = "";
		try{
			try {
				conn = ds.getConnection();				
			} catch (Exception e) {
				System.out.println("connect Exception : "+e.toString());
			}
			sql = "select * from member where NICKNAME='"+user+"'";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			System.out.println(sql);
			
			if(rs.next()) {
				MemberVo member = new MemberVo();
				member.setName(rs.getString("NAME"));
				member.setNickname(rs.getString("NICKNAME"));
				member.setHp(rs.getString("HP"));
				return member;
			}
			
			return null;
			
		}catch(Exception ex){
			System.out.println("보기 실패");
			ex.printStackTrace();
		}finally{
			if(rs!=null) try{rs.close();}catch(SQLException ex){}
			if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
			if(conn!=null) try{conn.close();}catch(SQLException ex){}
		}
		return null;
	}

	public boolean getUserUpdate(MemberVo member) {
		int result = 0;
		int num =0;
		String sql="";			
		
		try{
			try {
				conn = ds.getConnection();				
			} catch (Exception e) {
				System.out.println("connect Exception : "+e.toString());
			}

			sql="UPDATE member SET PASSWORD=MD5(?), HP=? WHERE NICKNAME='"+member.getNickname()+"'";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, member.getPwd());
			pstmt.setString(2, member.getHp());
			
			System.out.println("====================UpdateDB====================");
			System.out.println("1 : "+member.getPwd());
			System.out.println("2 : "+member.getHp());
	
			result=pstmt.executeUpdate();			
			if(result==0)return false;
			
			return true;
			
		}catch(Exception ex){
			System.out.println("수정 실패");
			ex.printStackTrace();
		}finally{
			if(rs!=null) try{rs.close();}catch(SQLException ex){}
			if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
			if(conn!=null) try{conn.close();}catch(SQLException ex){}
		}
		return false;
	}
}
